package com.xh.one2one.knowledgeSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.system.model.Dictionary;
import com.xh.bussiness.system.service.IDictionaryService;
import com.xh.one2one.knowledgeSystem.bean.request.KnowledgeTemplateRequest;
import com.xh.one2one.knowledgeSystem.model.KnowledgeTemplate;
import com.xh.one2one.knowledgeSystem.dao.KnowledgeTemplateMapper;
import com.xh.one2one.knowledgeSystem.service.IKnowledgeTemplateService;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xh.core.bean.XhPage;
import com.xh.core.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 知识元模板 服务实现类
 * </p>
 *
 * @author tw
 * @since 2020-12-22
 */
@Service
public class KnowledgeTemplateServiceImpl extends BaseServiceImpl<KnowledgeTemplateMapper, KnowledgeTemplate> implements IKnowledgeTemplateService {

    @Autowired
    private IDictionaryService dictionaryService;

    @Override
    public XhPage queryKnowledgeTemplatePage(XhPage page, KnowledgeTemplateRequest bean) {


        return (XhPage) this.dao.queryByPage(page,bean);
    }

    @Override
    public List queryKnowledgeTemplate(KnowledgeTemplateRequest bean) {
        QueryWrapper qw=new QueryWrapper<KnowledgeTemplate>();
        //拼装条件
        qw=prepareParams(bean, qw);
        if(StringUtils.isNotEmpty(bean.getSubjectId())){
            System.out.println(bean.getSubjectId());
            if(bean.getSubjectId().length() != 32){
               Dictionary subject = dictionaryService.queryByCode(bean.getSubjectId(),"SUBJECT");
                System.out.println(subject);
                bean.setSubjectId(subject.getId());
            }
            System.out.println(bean.getSubjectId());
            qw.eq("subject_id",bean.getSubjectId());
        }
        qw.eq("status",0);
        return this.dao.selectList(qw);
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(KnowledgeTemplateRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        return qw;
    }
}
